$(document).ready(function()
{

   //This is to remove the validation message if no poster image is present

	$('#term').focus(function()
    {
      var full = $("#poster").has("img").length ? true : false;
      
      if(full == false)
         $('#poster').empty();
    
    });
   
	//function definition

   	var getPoster = function()
   	{

        //Grab the movie title and store it in a variable
   		
   		
        var film = $('#term').val();

         //Check if the user has entered anything

        if(film == '')
        {
            //If the input field was empty, display a message
        	$('#suggest').empty();
        	$('#suggest_books').empty();
        	$('#suggest_music').empty();
            $('#poster').html("<h2 class='loading'>Ha! We haven't forgotten to validate the form! Please enter something.</h2>");
        } 
        else 
        {
        	
	        
	        // MUSIC
	        $.getJSON("http://developer.echonest.com/api/v4/artist/search?api_key=FILDTEOIK2HBORODV&name="+film, function(datos)
	        {
	        	console.log(datos);
	        	$('#suggest_music').empty();
	        	$('#suggest_music').append("<ul>");
	         	
	        	for(var i = 0; i < datos["response"]["artists"].length; i++)
	        	{
	        		var value = datos["response"]["artists"][i]["name"];
	        		$('#suggest_music').append("<li>"+value+"</li>");
	        	}
	        	$('#suggest_music').append("</ul>");
	        	
	        	
	        });

	        // BOOKS
	        $.getJSON("https://www.googleapis.com/books/v1/volumes?q="+film,function(datos)
	        {
	        	$('#suggest_books').empty();
	        	
	        	$('#suggest_books').append("<ul>");
	        	
	        	for(var i = 0; i < datos["items"].length; i++)
	        	{
	        		var value = datos["items"][i]["volumeInfo"]["title"];
	        		$('#suggest_books').append("<li>"+value+"</li>");
	        	}
	        	
	        	$('#suggest_books').append("</ul>");
	        	
	        	
	        });
	        
	        // MOVIES
	        $.getJSON("http://api.themoviedb.org/2.1/Movie.search/en/json/5f6971a37419c0c775167c169f5d2b9f/" + film + "?callback=?", function(json) 
	        {

               //TMDb is nice enough to return a message if nothing was found, so we can base our if statement on this information
               if (json != "Nothing found.")
               {
					console.log(json);
					
                  //Display the poster and a message announcing the result
					$('#suggest').empty();
					$('#poster').html('<h2 class="loading">Well, gee whiz! We found you a poster, skip!</h2><img id="thePoster" src=' + json[0].posters[0].image.url + ' />');
                    
                    $('#suggest').append("<ul>");
                     
                    for(var i = 0; i < json.length; i++)
                    	$('#suggest').append("<li>"+json[i].original_name+" </li>");

                    $('#suggest').append("</ul>");
                   
               } 
               else 
               {
            	   	$('#suggest').empty();
            	   	$('#suggest').append("No se han encontrado resultados.");
               }                   
	        });
        }

        return false;
   }

   //Because we've wrapped the JSON code in a function, we can call it on mouse click or on a hit of the Return button while in the input field

   $('#search').click(getPoster);

   $('#term').keyup(function(event)
   {

       //if(event.keyCode == 13){
	   
	   getPoster();

       //}

   });

});

